package cn.wit.summit.chwan.ui.activity;

import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.facebook.drawee.view.SimpleDraweeView;

import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.request.TreviFountainRequest;
import cn.wit.summit.chwan.models.response.ResponseBase;
import cn.wit.summit.chwan.models.response.TreviFountainResponse;
import cn.wit.summit.chwan.net.BuildRequestUtils;
import cn.wit.summit.chwan.net.NetCallBack;
import cn.wit.summit.chwan.net.okhttplib.OkHttpUtil;
import cn.wit.summit.chwan.retrofit.ApiUtil;
import cn.wit.summit.chwan.retrofit.MyCallback;
import cn.wit.summit.chwan.retrofit.RetrofitManager;
import cn.wit.summit.chwan.ui.base.MyActivity;
import cn.wit.summit.chwan.ui.base.MyApplication;
import cn.wit.summit.chwan.utils.AccountBean;
import cn.wit.summit.chwan.utils.AccountUtil;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.StringUtils;
import cn.wit.summit.chwan.widgets.CurrencyPromptDialog;
import retrofit2.Retrofit;

/**
 * 许愿池
 * Created by Alan on 2018/4/20.
 */

public class TreviFountainActivity extends MyActivity {

    @Bind(R.id.gameNameTx)
    EditText gameNameTx;
    @Bind(R.id.gameDetailTx)
    EditText gameDetailTx;
    @Bind(R.id.adIv)
    SimpleDraweeView adIv;

    private int uid;
    private String account;
    private String token;

    private RetrofitManager retrofitManager;
    private Retrofit retrofit;
    private ApiUtil api;

    private LinearLayout.LayoutParams paramsImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trevi_fountain);
        ButterKnife.bind(this);

        Display display = getWindowManager().getDefaultDisplay();
        int w = display.getWidth();
        int h = w * 276 / 720;
        paramsImage = new LinearLayout.LayoutParams(w, h);
        adIv.setLayoutParams(paramsImage);

        String url = MyApplication.cfgPlug.getValue_wish_ad_pic();
        if (StringUtils.isEmpty(url)) {
            adIv.setVisibility(View.GONE);
        } else {
            adIv.setVisibility(View.VISIBLE);
            adIv.setImageURI(url);
        }

        hideTitleBar();

        retrofitManager = RetrofitManager.getInstance(mContext);
        retrofit = retrofitManager.getRetrofit();
        api = retrofit.create(ApiUtil.class);
    }

    @OnClick({R.id.treviBarBackIv, R.id.treviBarRightTx, R.id.adIv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.treviBarBackIv:
                finish();
                break;
            case R.id.treviBarRightTx:
                hideSoftInput(view);
                submitTheMessage();
                break;
            case R.id.adIv:
                if (AccountUtil.getInstance().getUidNotToLogin(mContext) == 0) return;
                IntentUtil.getInstance().goShareWebActivity(mContext, MyApplication.cfgPlug.getValue_game_upload_url_v3());
                break;
        }
    }

    private void submitTheMessage() {
        AccountBean accountBean = AccountUtil.getInstance().getAccountData();
        if (accountBean == null) {
            IntentUtil.getInstance().goLoginActivity(TreviFountainActivity.this);
            return;
        }
        uid = accountBean.getUid();
        account = accountBean.getAccount();
        token = accountBean.getToken();
        String name = gameNameTx.getText().toString().trim();
        String detail = gameDetailTx.getText().toString().trim();
        if (StringUtils.isEmpty(name)) {
            showToast(getResources().getString(R.string.hint_gamename_null_str));
            return;
        }
        if (StringUtils.isEmpty(detail)) {
            showToast(getResources().getString(R.string.hint_gameinfo_null_str));
            return;
        }
        sendMessage(name, detail);
    }

    private void sendMessage(String name, String detail) {
        showLoading(getResources().getString(R.string.trevi_sub_str));
        TreviFountainRequest mTreviFountainRequest = retrofitManager.sendTreviGamesRequest(uid+"", account, token, name, detail);
        api.sendTreviGames(mTreviFountainRequest).enqueue(new MyCallback<ResponseBase<TreviFountainResponse>>() {
            @Override
            public void onSuccess(ResponseBase<TreviFountainResponse> model) {
                cancelLoading();
                showDialog();
            }

            @Override
            public void onFailure(int code, String msg) {
                cancelLoading();
                showToast(msg);
            }
        });
    }

    private void showDialog() {
        final CurrencyPromptDialog dialog = new CurrencyPromptDialog(mContext);
        dialog.show();
        dialog.setTitle(getResources().getString(R.string.trevi_dialog_title_str));
        dialog.setMsg(getResources().getString(R.string.trevi_dialog_msg_str));
        dialog.setCancleBtn(getResources().getString(R.string.trevi_dialog_bt_str), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                gameNameTx.setText("");
                gameDetailTx.setText("");
                gameNameTx.clearFocus();
                gameDetailTx.clearFocus();
            }
        });
    }
}
